//****************************************************************************
//
// lineIR_task.c
//
// Using PmodLS1 and the Fairchild QRB1134 IR proximity sensor, this 'task' 
// routine is inteded for use with the qs-autonomous simple task scheduler.
//
// There are 3 IR sensors along a straight edge accross the front of 
// EVALBOT. They are labelled 0, 1, 2 from left to right and hook into GPIO
// ports J4, J5, and J6, respectively. They output a logic 1 (+3.3V) when 
// actively sensing IR reflections. Else, they output a logic 0 (0V).
//
// The line has the following constraints:
//   - the line absorbs IR, while surrounding material reflects
//   - it's width is roughly the separation between IR sensors
//   - corner might be sharper than 90 degrees
//   - smooth curves are allowed
//
//****************************************************************************

#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "drivers/io.h"

#include "lineIR.h"
#include "helper.h"



LineReading GetLineReading()
{
	int portValue = GPIOPinRead(GPIO_PORTJ_BASE, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6);
	char sensVal = (char)(portValue >> 4);
	sensVal = sensVal & 0x07; // only keep lower 3 bits.

	return (LineReading)sensVal;
}

void LineIRInit(void)
{
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ);
	GPIOPinTypeGPIOInput(GPIO_PORTJ_BASE, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6);
}

void CalibrateLineIR()
{
    while(PushButtonGetStatus(BUTTON_1) != false)
    {
    	LineReading reading = GetLineReading();
    	displayLineReading("Calibration", reading);
    }

}


